package game.entity.ground;

import edu.monash.fit2099.engine.*;
import game.entity.action.EatAction;
import game.entity.action.SearchAction;
import game.entity.actor.Brachiosaur;
import game.entity.actor.Player;
import game.entity.actor.Stegosaur;
import game.entity.item.Fruit;
import game.interfaces.Eatable;
import game.util.RandomUtil;

import java.util.ArrayList;
import java.util.List;

public class Bush extends Ground implements Eatable {
    private int age = 0;
    private List<Fruit> fruits = new ArrayList<>();
    //找到水果的概率
    private final int FIND_FRUIT_RATE = 60;
    //生长水果的概率
    private final int GEN_FRUIT_RATE = 90;
    public String name;

    public Bush() {
        super('o');
        this.name = "bush"+RandomUtil.randomInt();
    }


    @Override
    public void tick(Location location) {
        //产水果
        if (RandomUtil.shotOn(GEN_FRUIT_RATE) && !hasFruit()) {
            fruits.add(new Fruit("fruit" + RandomUtil.randomInt()));
        }
        //如果Brachiosaurs踩在灌木丛上，有50%的机会杀死灌木丛。
        if(RandomUtil.shotOn(50) && location.getActor() instanceof Brachiosaur){
            location.setGround(new Dirt());
        }
        age++;
    }

    @Override
    public Actions allowableActions(Actor actor, Location location, String direction) {
//        应该返回搜索
        Actions actions = new Actions();
        if(actor instanceof Player){
            actions.add(new SearchAction(FIND_FRUIT_RATE, this));
        }
        if(actor instanceof Stegosaur && hasFruit()){
            actions.add(new EatAction(fruits.get(0)));
        }
        return actions;
    }

    @Override
    public boolean hasFruit() {
        return fruits.size() > 0 ? true : false;
    }

    @Override
    public List<Fruit> getFruits() {
        return fruits;
    }

    @Override
    public void removeFruit(Fruit fruit) {
        fruits.remove(fruit);
    }
}
